﻿/// <reference path="../../../Scripts/jquery-1.4.1-vsdoc.js" />

var DatePicker = function (options) {

    this.AssociatedControl1 = $({});

    this.AssociatedControl2 = $({});

    this.HasCreate = false;

    this.createDatePicker = function (options) {

        var ref = this;

        var defaults = {
            ActiveEvent: "click",
            FormatString: "yyyy-MM-dd",
            OnSelected: function () { },
            Skin: "default",
            CssClass: "Wdate"
        };

        var opt = $.extend(defaults, options);

        if (opt.AssociatedControl1ID)
            this.AssociatedControl1 = $("#" + opt.AssociatedControl1ID);
        else
            this.AssociatedControl1 = $(opt.AssociatedControl1);

        if (opt.AssociatedControl2ID)
            this.AssociatedControl2 = $("#" + opt.AssociatedControl2ID);
        else
            this.AssociatedControl2 = $(opt.AssociatedControl2);

        if (this.AssociatedControl1.length == 0) return;
        //  throw new Error("无效参数,无法获取要绑定项!");

        //   if (this.AssociatedControl1.attr("readOnly")) return;

        this.AssociatedControl1.addClass(opt.CssClass); //添加容器控件1样式

        if (this.AssociatedControl2 && this.AssociatedControl2.length > 0) {
            this.AssociatedControl2.addClass(opt.CssClass); //添加容器控件2样式

            this.AssociatedControl1.bind(opt.ActiveEvent, function () {
                var obj = { onpicked: opt.OnSelected, skin: opt.Skin, dateFmt: opt.FormatString, maxDate: '#F{$dp.$D(\'' + ref.AssociatedControl2.attr("Id") + '\')||\'2020-10-01\'}' };
                WdatePicker(obj);
            });
            //   if (this.AssociatedControl2.attr("readOnly")) return; //如果控件为只读时不绑定事件
            this.AssociatedControl2.bind(opt.ActiveEvent, function () {
                var obj = { onpicked: opt.OnSelected, skin: opt.Skin, dateFmt: opt.FormatString, minDate: '#F{$dp.$D(\'' + ref.AssociatedControl1.attr("Id") + '\')}', maxDate: '2020-10-01' };
                WdatePicker(obj);
            });
        } else {
            this.AssociatedControl1.bind(opt.ActiveEvent, function () {
                WdatePicker({ onpicked: opt.OnSelected, skin: opt.Skin, dateFmt: opt.FormatString });
            });
        }
        this.HasCreate = true;
        return { AssociatedControl1: this.AssociatedControl1, AssociatedControl2: this.AssociatedControl2 };

    }

    this.removeDatePicker = function () {
 
        if (this.AssociatedControl1)
            remove(this.AssociatedControl1);

        if (this.AssociatedControl2)
            remove(this.AssociatedControl2);

        function remove(c) {
            c.val("");
            c.removeClass();
            c.unbind();
        }
        this.HasCreate = false;
    }

    this.getDate = function () {
        if (!this.HasCreate) return "";
        var sDate1 = this.AssociatedControl1.val();
        var sDate2 = this.AssociatedControl2.val();
   
        if (sDate1 != "" || sDate2 != "")
            return sDate1 + "|" + sDate2;

        return "";
    }

    if (options) this.createDatePicker(options);

}

 